home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / !best001.c next >
C/C++ Source or Header  |  1994-05-12  |  28KB  |  720 lines

  1. /*==========================================================================
  2.  *
  3.  *  !BEST001.C                                       Tuesday, April 12, 1994
  4.  *
  5.  *  con_ str_ fil_ counter counter_reg dos_shell
  6.  *  supplementary source file 1 for The BESTLibrary
  7.  *
  8.  *  Authored independently by George Vanous
  9.  *
  10.  *==========================================================================*/
  11.  
  12.  
  13. /* ------------------------------------------------------------------------ */
  14. /* ----------------------------  INCLUDE FILES  --------------------------- */
  15.  
  16. #include <dir.h>
  17. #include <alloc.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <process.h>
  21. #include "!bestlib.h"
  22.  
  23. /* ------------------------------------------------------------------------ */
  24. /* ------------------------------  MESSAGES  ------------------------------ */
  25.  
  26. #define MSG01 "\nThis is your first time using %s\nI, %s, hope you enjoy this program\n\n"
  27. #define MSG02 "\n%s has been used %d time"
  28. #define MSG03 "\nThere is %ld bytes of free RAM now available to %s\n"
  29. #define MSG04 "\nThis copy of %s is registered to %s\n"
  30. #define MSG05 "\nThis is the master copy of %s\nIt is registered to its author, %s\n"
  31. #define ERR01 "\n!!! ERROR !!!  I cannot find the data file %s\n"
  32.  
  33. /* ------------------------------------------------------------------------ */
  34.  
  35.  
  36. /*----------------------------------------------------------------------------
  37.  * Shell to DOS.
  38.  *
  39.  * RETURNS:
  40.  * = pointer to an error message
  41.  * = NULL if no error message
  42.  */
  43. char *dos_shell(void)
  44. {
  45.   int error;
  46.   char *commandpath, *textstring;
  47.   struct ffblk ffblk;
  48.  
  49.   if ((commandpath = getenv("COMSPEC")) == NULL &&
  50.      (commandpath = searchpath("COMMAND.COM")) == NULL) {
  51.     if ((error = findfirst(commandpath = "COMMAND.COM", &ffblk, 0)) == NULL &&
  52.         (error = findfirst(commandpath = "C:\COMMAND.COM", &ffblk, 0)) == NULL &&
  53.         (error = findfirst(commandpath = "C:\DOS\COMMAND.COM", &ffblk, 0)) == NULL &&
  54.         (error = findfirst(commandpath = "C:\BIN\COMMAND.COM", &ffblk, 0)) == NULL) {
  55.       textstring = "Cannot find file COMMAND.COM -- initialize COMSPEC=[path]COMMAND.COM";
  56.       return( textstring );
  57.     }
  58.   }
  59.   video_set(TEXT);
  60.   error = spawnl(P_WAIT, commandpath, commandpath, NULL);
  61.  
  62.   if (error == -1) {
  63.     switch(errno) {
  64.     case EINVAL : textstring = "Bad argument passed to COMMAND.COM"; break;
  65.     case E2BIG  : textstring = "Too many arguments passed to COMMAND.COM"; break;
  66.     case ENOEXEC: textstring = "Internal error -- exitting strongly suggested"; break;
  67.     case ENOMEM : textstring = "Out of memory -- cannot shell to DOS"; break;
  68.     case ENOENT : textstring = "Cannot find file COMMAND.COM -- initialize COMSPEC=[path]COMMAND.COM";
  69.     }
  70.   }
  71.   else if (!error) textstring = NULL;
  72.   return( textstring );
  73. }
  74.  
  75. /*----------------------------------------------------------------------------
  76.  * Read, print, and increment the number of program executions.
  77.  * Print a comment, depending on the current execution number.
  78.  * Print the amount of available memory.
  79.  *
  80.  * "filename" - name of the text data file to read the number of program
  81.  *              executions from (this number must be the first byte)
  82.  *            - writes the incremented number of executions back to the file
  83.  * "title"    - name of the program
  84.  * "author"   - name of the author of the program
  85.  * "msgs"     - one comment will be randomly chosen from this array of
  86.  *              messages
  87.  * "msg_num"  - number of messages present
  88.  *
  89.  * RETURNS:
  90.  * = file handle of the opened data file
  91.  */
  92. FILE *fil_count(char *filename, char *title, char *author, char *msgs[], word msg_num)
  93. {
  94.   word counter;                        // usage counter
  95.   FILE *f;                             // file handle
  96.  
  97.   DASHES;                              // print one line of dashes
  98.   if ((f = fopen(filename, "r+b")) == NULL) {
  99.     fprintf(stderr, ERR01, filename);  // file could not be opened
  100.     exit(1);                           // abort with ERRORLEVEL 1
  101.   }
  102.  
  103.   /* the first byte of the file is the usage counter -- get it */
  104.   fread(&counter, sizeof(counter), 1, f);
  105.  
  106.   /* increment the counter and print the correct message */
  107.   if (counter++ == 0)
  108.     printf(MSG01, title, author);      // print "first time used" message
  109.   else {
  110.     printf(MSG02, title, counter);     // print number of program uses
  111.     if (counter != 1)                  // if uses do not equal 1 then
  112.       printf("s");                     //   pluralise
  113.   }
  114.   DOUBLESPACE;
  115.  
  116.   /* write the incremented counter back to the file */
  117.   rewind(f);                           // go to beginning of file
  118.   fwrite(&counter, sizeof(counter), 1, f);
  119.  
  120.   // the "fseek" is REQUIRED for the next "fread" to work???
  121.   fseek(f, 0L, SEEK_CUR);
  122.  
  123.   printf(msgs[random(msg_num)], title);
  124.   printf(MSG03, coreleft(), title);    // print amount of available memory
  125.   return( f );                         // return file handle
  126. }
  127.  
  128. /*----------------------------------------------------------------------------
  129.  * Read, print, and increment the number of program executions.
  130.  * Print to whom this program is registered.
  131.  * Print the amount of available memory.
  132.  *
  133.  * "filename"  - name of the text data file to read the number of program
  134.  *               executions from (this number must be the first byte)
  135.  *             - writes the incremented number of executions back to the file
  136.  * "title"     - name of the program
  137.  * "author"    - name of the author of the program
  138.  * "registree" - name of the person to whom this program is registered
  139.  *
  140.  * RETURNS:
  141.  * = file handle of the opened data file
  142.  */
  143. FILE *fil_count_reg(char *filename, char *title, char *author, char *registree)
  144. {
  145.   word counter;                        // usage counter
  146.   FILE *f;                             // file handle
  147.  
  148.   DASHES;                              // print one line of dashes
  149.   if ((f = fopen(filename, "r+b")) == NULL) {
  150.     fprintf(stderr, ERR01, filename);  // file could not be opened
  151.     exit(1);                           // abort with ERRORLEVEL 1
  152.   }
  153.  
  154.   /* the first byte of the file is the usage counter -- get it */
  155.   fread(&counter, sizeof(counter), 1, f);
  156.  
  157.   /* increment the counter and print the correct message */
  158.   if (counter++ == 0)
  159.     printf(MSG01, title, author);      // print "first time used" message
  160.   else {
  161.     printf(MSG02, title, counter);     // print number of program uses
  162.     if (counter != 1)                  // if uses do not equal 1 then
  163.       printf("s");                     //   pluralise
  164.   }
  165.   DOUBLESPACE;
  166.  
  167.   /* write the incremented counter back to the file */
  168.   rewind(f);                           // go to beginning of file
  169.   fwrite(&counter, sizeof(counter), 1, f);
  170.  
  171.   // the "fseek" is REQUIRED for the next "fread" to work???
  172.   fseek(f, 0L, SEEK_CUR);
  173.  
  174.   if (!str_cmp(author, registree))     // if this is registered to author then
  175.     printf(MSG05, title, author);      //   print master copy message
  176.   else                                 // else
  177.     printf(MSG04, title, registree);   //   print to whom this is registered
  178.  
  179.   printf(MSG03, coreleft(), title);    // print amount of available memory
  180.   return( f );                         // return file handle
  181. }
  182.  
  183. /*----------------------------------------------------------------------------
  184.  * Return the length of a file, in bytes.
  185.  *
  186.  * "f" - file handle of file to return length of
  187.  *
  188.  * RETURNS:
  189.  * = length of file in bytes
  190.  */
  191. long fil_len(FILE *f)
  192. {
  193.   long curpos, length;
  194.  
  195.   curpos = ftell(f);                   // save current position
  196.   fseek(f, 0L, SEEK_END);              // go to EOF
  197.   length = ftell(f);                   // get EOF position relative to start
  198.   fseek(f, curpos, SEEK_SET);          // go back to original position
  199.   return( length );                    // return length of file, in bytes
  200. }
  201.  
  202. /*----------------------------------------------------------------------------
  203.  * Read the next string from the current file position.
  204.  *
  205.  * "size" - initial size of text input buffer
  206.  * "f"